package org.neuedu.practice02;

import java.text.SimpleDateFormat;
import java.util.*;

public class Main {

    public static School school = new School();

    public static void signIn(int id) {
        //签到的学生
        Student student = school.getStudentById(id);
        //签到的信息
        Map<String, List<SignInfo>> signInfoMap = school.map;
        //今天是否有签到的时间
        Date date =new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        String today = dateFormat.format(date);

        if(student==null){
            System.out.println("不是本校学生");
        }else {
            //封装学生信息
            SignInfo studentInfo = new SignInfo();
            studentInfo.setId(student.getId());
            studentInfo.setStart(new Date());
            studentInfo.setEnd(null);

            if (!signInfoMap.containsKey(today)){
                List<SignInfo> todayStudents=new ArrayList<>();
                //添加学生信息到今天签到集合
                todayStudents.add(studentInfo);
                signInfoMap.put(today,todayStudents);
                System.out.println("签到成功");
            }else {
                boolean flag = false;
                for(SignInfo stuInfo:signInfoMap.get(today)){
                    if (stuInfo.getId()==student.getId()){
                        System.out.println("已经签过到了");
                        break;
                    }else {
                        flag=true;
                    }
                }
                if (flag){
                    //添加学生信息到今天签到集合
                    signInfoMap.get(today).add(studentInfo);
                    System.out.println("签到成功");
                }
            }
        }
    }

    public static void signOut(int id) {
        Date date =new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        String today = dateFormat.format(date);

        Student student = school.getStudentById(id);

        if (school.map.size()==0){
            System.out.println("今天无人签到");
        }else if (student==null){
            System.out.println("您不是本校学生");
        }else {
            //依据id查询今天是否已经签到
            boolean flag=true;
            for ( SignInfo s:school.map.get(today)) {
                if (s.getId()==id) {
                    s.setEnd(new Date());
                    System.out.println("签退成功");
                    flag=false;
                }
            }
            if (flag){
                System.out.println("您今天未签到");
            }
        }
    }

    public static void showInfo() {
        Date date =new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        String today = dateFormat.format(date);

        if (school.map.size()==0){
            System.out.println("今天无人签到");
        }else {
            for(SignInfo student:school.map.get(today)){
                System.out.println(student);
            }
        }
    }

    public static void main(String[] args) {
        school.studentList.add(new Student(1001, "tom"));
        school.studentList.add(new Student(1002, "mike"));
        school.studentList.add(new Student(1003, "ella"));
        school.studentList.add(new Student(1004, "lucy"));
        school.studentList.add(new Student(1005, "bob"));



        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("欢迎使用打卡系统");
            System.out.println("1---------签到");
            System.out.println("2---------签退");
            System.out.println("3---------查看打卡信息");
            System.out.println("4---------退出");
            String option = scanner.nextLine();
            switch (option){
                case "1":
                    System.out.println("请输入学号：");
                    String id1 = scanner.nextLine();
                    signIn(Integer.valueOf(id1));
                    break;
                case "2":
                    System.out.println("请输入学号：");
                    String id2 = scanner.nextLine();
                    signOut(Integer.valueOf(id2));
                    break;
                case "3":
                    showInfo();
                    break;
                case "4":
                    System.out.println("再见！");
                    return;
                default:
                    System.out.println("请重新输入");
            }
        }

    }
}
